package com.moneywelcome.investassembly.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.moneywelcome.goldfacade.GoldQueryApi;
import com.moneywelcome.http.ServletUtil;
import com.moneywelcome.investfacade.InvestIncomeApi;
import com.moneywelcome.investfacade.dto.DividendRecordDto;
import com.moneywelcome.investfacade.dto.FinancialDto;
import com.moneywelcome.investfacade.dto.IncomeRecordDto;
import com.moneywelcome.investfacade.dto.UserFinancialDto;
import com.moneywelcome.investfacade.vo.RecordPageVo;
import com.moneywelcome.orderfacade.FinancialApi;
import com.moneywelcome.orderfacade.OrderQueryApi;
import com.moneywelcome.orderfacade.dto.DividendOrderDto;
import com.moneywelcome.orderfacade.dto.ProfitDayDto;
import com.moneywelcome.orderfacade.vo.OrderPageVo;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.Result;
import com.moneywelcome.validate.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(tags = "用户投资收益服务")
public class InvestIncomeApiImpl implements InvestIncomeApi {
    @Autowired
    private GoldQueryApi gqApi;
    @Autowired
    private FinancialApi financialApi;
    @Autowired
    private OrderQueryApi oqApi;
    @ApiOperation("分页查询用户收益纪录")
    @Override
    public Result<PageResult<IncomeRecordDto>> incomeRecord(RecordPageVo recordPageVo) {
        log.info("查询收益记录{}",recordPageVo);
        ValidationUtils.validate(recordPageVo);
        //获取用户id
        Integer userId = Integer.valueOf(ServletUtil.getHttpServletRequest().getHeader("userId"));
        OrderPageVo orderPageVo = BeanUtil.copyProperties(recordPageVo, OrderPageVo.class);
        orderPageVo.setUserId(userId);
        PageResult<ProfitDayDto> data = oqApi.prrofitRecord(orderPageVo).getData();
        PageResult<IncomeRecordDto> pageResult = BeanUtil.copyProperties(data, PageResult.class);
        pageResult.setData(BeanUtil.copyToList(data.getData(),IncomeRecordDto.class));
        return Result.success(pageResult);
    }
    @Override
    @ApiOperation("分页查询用户持仓")
    public Result<PageResult<UserFinancialDto>> getUserFinancial(RecordPageVo recordPageVo) {
        log.info("查询持仓记录{}",recordPageVo);
        ValidationUtils.validate(recordPageVo);
        OrderPageVo orderPageVo = BeanUtil.copyProperties(recordPageVo, OrderPageVo.class);
        Integer userId = Integer.valueOf(ServletUtil.getHttpServletRequest().getHeader("userId"));
        orderPageVo.setUserId(userId);
        PageResult<com.moneywelcome.orderfacade.dto.UserFinancialDto> data = financialApi.financialQuery(orderPageVo).getData();
        PageResult<UserFinancialDto> pageResult = BeanUtil.copyProperties(data, PageResult.class);
        pageResult.setData(BeanUtil.copyToList(data.getData(),UserFinancialDto.class));
        return Result.success(pageResult);
    }
    @ApiOperation("分页查询用户分红记录")
    @Override
    public Result<PageResult<DividendRecordDto>> dividendRecord(RecordPageVo recordPageVo) {
        log.info("查分红记录{}",recordPageVo);
        ValidationUtils.validate(recordPageVo);
        OrderPageVo orderPageVo = BeanUtil.copyProperties(recordPageVo, OrderPageVo.class);
        Integer userId = Integer.valueOf(ServletUtil.getHttpServletRequest().getHeader("userId"));
        orderPageVo.setUserId(userId);
        PageResult<DividendOrderDto> data = oqApi.dividendRecord(orderPageVo).getData();
        PageResult<DividendRecordDto> pageResult = BeanUtil.copyProperties(data, PageResult.class);
        pageResult.setData(BeanUtil.copyToList(data.getData(),DividendRecordDto.class));
        return  Result.success(pageResult);
    }
}
